package com.mmw.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmw.crm.bo.ContractPageBo;
import com.mmw.crm.entity.Contract;
import com.mmw.crm.result.Result;
import com.mmw.crm.service.ContractService;
import com.mmw.crm.vo.ContractSaveEntityVo;
import com.mmw.crm.vo.ContractSaveVo;
import com.mmw.crm.vo.CustomerVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class ContractController {

    @Resource
    ContractService contractService;

    @PostMapping("CrmContract/deleteByIds")
    public Result crmContactsDeleteByIds(@RequestParam List<Integer> contractIds){
        contractService.removeByIds(contractIds);
        return Result.build(null,null,0);
    }

    @PostMapping("/CrmContract/queryPageList")
    public Result crmContractQueryPageList(@RequestBody CustomerVo customerVo){
        Integer page = customerVo.getPage();
        Integer limit = customerVo.getLimit();
        String search = customerVo.getSearch();

        LambdaQueryWrapper<Contract> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)){
            wrapper.like(Contract::getName,search);
        }
        Page<Contract> pageInfo = new Page<>(page,limit);
        contractService.page(pageInfo,wrapper);
        List<Contract> records = pageInfo.getRecords();

        ContractPageBo contractPageBo = new ContractPageBo();
        contractPageBo.setTotalRow(records.size());
        contractPageBo.setList(records);

        return Result.build(contractPageBo,null,0);

    }
    @PostMapping("examine/queryExaminStep")
    public String examineQueryExaminStep(){
        String res = contractService.QueryExaminStep();
        return res;
    }
    @PostMapping("/CrmContract/saveAndUpdate")
    public Result crmContractSaveAndUpdate(@RequestBody ContractSaveVo contractSaveVo){
        ContractSaveEntityVo entity = contractSaveVo.getEntity();

        Contract contract = new Contract();
        contract.setName(entity.getName());
        contract.setBusinessId(entity.getBusiness_id());
        contract.setCompanyUserId(entity.getCompany_user_id());
        contract.setContactsId(entity.getContacts_id());
        contract.setEndTime(entity.getEnd_time());
        contract.setMoney(entity.getMoney());
        contract.setNum(entity.getNum());
        contract.setOrderDate(entity.getOrder_date());
        contract.setRemark(entity.getRemark());
        contract.setStartTime(entity.getStart_time());
        contract.setTotalPrice(entity.getTotalPrice());
        contract.setBatchId("1");
        contract.setCreateTime(new Date());
        contract.setCreateUserId(1L);
        contractService.save(contract);

        return Result.build(null,null,0);
    }

    @PostMapping("/system/user/queryUserList")
    public String userQueryUserList(@RequestParam Integer pageType){
        String res = contractService.userQueryUserList(pageType);
        return res;
    }

}
